<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="isShowDialog = true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="List">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="{row}">
                  <el-button size="small" type="success" @click="assignPerm(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="btnUp(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="btndel(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                layout="prev,pager,next"
                :total="total"
                :page-size="pageConfig.pagesize"
                @current-change="currentChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyInfo.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyInfo.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyInfo.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyInfo.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>

          <!-- 分配权限 -->
          <el-dialog title="分配权限" :visible="showPermDialog" @close="btnPermCancel">
            <!-- 权限是一颗树 -->
            <!-- 将数据绑定到组件上 -->
            <!-- check-strictly 如果为true 那表示父子勾选时  不互相关联 如果为false就互相关联 -->
            <!-- id作为唯一标识 -->
            <el-tree
              ref="permTree"
              :data="permData"
              :props="defaultProps"
              :show-checkbox="true"
              :check-strictly="true"
              :default-expand-all="true"
              :default-checked-keys="selectCheck"
              node-key="id"
            />
            <!-- 确定 取消 -->
            <el-row slot="footer" type="flex" justify="center">
              <el-col :span="6">
                <el-button type="primary" size="small" @click="btnPermOk">确定</el-button>
                <el-button size="small" @click="btnPermCancel">取消</el-button>
              </el-col>
            </el-row>
          </el-dialog>

        </el-tabs>
      </el-card>
    </div>
    <!-- sync 属性 -->
    <Dialog ref="dialog" :is-show-dialog.sync="isShowDialog" @addRole="getSysroleList" />
  </div>
</template>

<script>
import Dialog from './components/dialog.vue'
import { getSysroleList, getcompanyInfo, delSysRole, assignPrem, getRoleList } from '@/api/setting'
import { getPermissionList } from '@/api/permisson'
import { listToreeData } from '@/utils'
export default {
  components: { Dialog },
  data() {
    return {
      List: [], // 接收角色数据
      companyInfo: {}, // 公司信息
      pageConfig: {
        page: 1,
        pagesize: 3
      },
      total: 0,
      isShowDialog: false,
      // 分配权限
      showPermDialog: false, // 弹出权限分配
      permData: [], // 专门用来接收权限数据 树形数据
      selectCheck: [], // 定义一个数组来接收 已经选中的节点
      roleId: null, // 用来记录分配角色的id
      defaultProps: {
        label: 'name'
      }

    }
  },
  created() {
    this.getSysroleList()
    this.getcompanyInfo()
  },
  methods: {
    // 点击分配权限
    async assignPerm(id) {
      this.permData = listToreeData(await getPermissionList(), '0')
      this.roleId = id
      const { permIds } = await getRoleList(id)
      this.showPermDialog = true
      // this.selectCheck = permIds // 来接收获取到的当前数据
      this.$nextTick(() => {
        this.$refs.permTree.setCheckedKeys(permIds)
      })
    },
    // 分配权限的取消按钮
    btnPermCancel() {
      // 清除数据
      // this.selectCheck = []
      // this.$nextTick(() => {
      //   this.$refs.permTree.setCheckedKeys([])
      // })
      this.$refs.permTree.setCheckedKeys([])
      // 关闭弹窗
      this.showPermDialog = false
    },
    // 分配权限的确定按钮
    async btnPermOk() {
      await assignPrem({ permIds: this.$refs.permTree.getCheckedKeys(), id: this.roleId })
      this.$message.success('给角色分配权限成功')
      this.showPermDialog = false
    },

    // 角色信息
    async getSysroleList() {
      const { rows, total } = await getSysroleList(this.pageConfig)
      console.log(rows)
      this.List = rows
      // 监听 total
      this.total = total
    },
    // 公司信息
    async getcompanyInfo(id) {
      this.companyInfo = await getcompanyInfo(id)
    },
    // 分页功能的实现
    currentChange(newPage) {
      this.pageConfig.page = newPage
      this.getSysroleList()
    },
    // 根据id删除角色数据
    async btndel(id) {
      // 弹出提示框
      await this.$confirm('是否确定删除')
      // 发起请求
      await delSysRole(id)
      // 提示用户
      this.$message('删除成功')
      // 更新页面
      this.getSysroleList()
    },
    // 根据id编剧角色数据
    async btnUp(id) {
      // 回显
      this.$refs.dialog.getRoleList(id)
      this.isShowDialog = true
    }
  }

}
</script>

<style>

</style>
